package net.xiduth.game.player.dialogues.impl;

import net.xiduth.game.player.content.grandexchange.GrandExchangeOffer;
import net.xiduth.game.player.content.grandexchange.WorldOffers;
import net.xiduth.game.player.dialogues.Dialogue;

/**
 * @author Tyluur <itstyluur@gmail.com>
 * @since 2012-08-17
 */
public class GrandExchangeCollect extends Dialogue {

	int slot;

	@Override
	public void start() {
		slot = (Integer) parameters[0];
		sendOptionsDialogue("Select an option", "Claim", "Abort");
	}

	@Override
	public void run(int interfaceId, int option) {
		end();
		GrandExchangeOffer offer = player.getGrandExchange().getOffers()[slot];
		if (offer == null) {
			player.sendMessage("This offer has already been processed...");
			player.getPackets().sendGrandExchangeBar(player, (Integer) player.getAttributes().get("grand_exchange_slot"), 8, 0, 0, 0, 0);
			return;
		}
		switch(option) {
		case OPTION_1:
			switch(offer.getType()) {
			case BUY:
				if (player.getInventory().getFreeSlots() == 0) {
					player.sendMessage("You need some free inventory slots...");
					return;
				}
				if (offer.getAmountCompleted() == offer.getOfferedAmount()) {
					if (offer.getOfferedAmount() > 1) {
						player.getInventory().addItem(offer.getItem().getDefinitions().getCertId() == -1 ? offer.getItem().getId() : offer.getItem().getDefinitions().getCertId(), offer.getAmountCompleted());
					} else
						player.getInventory().addItem(offer.getItem().getId(), offer.getAmountCompleted());
					offer.setAmountCompleted(0);
					player.getGrandExchange().getOffers()[slot] = null;
					WorldOffers.getOffers().remove(offer);
					player.getGrandExchange().sendExchangeScreen();
					player.getPackets().sendGrandExchangeBar(
							player,
							(Integer) player.getAttributes().get(
									"grand_exchange_slot"), 8,
									offer.getItem().getId(), offer.getOfferedPrice(),
									offer.getOfferedAmount(), offer.getOfferedAmount());
				} else {
					if (offer.getOfferedAmount() > 1) {
						if (!offer.getItem().getDefinitions().isStackable())
							player.getInventory().addItem(offer.getItem().getDefinitions().getCertId() == -1 ? offer.getItem().getId() : offer.getItem().getDefinitions().getCertId(), offer.getAmountCompleted());
						else
							player.getInventory().addItem(offer.getItem().getDefinitions().getCertId() == -1 ? offer.getItem().getId() : offer.getItem().getDefinitions().getCertId(), offer.getAmountCompleted());
					} else
						player.getInventory().addItem(offer.getItem().getDefinitions().getCertId() == -1 ? offer.getItem().getId() : offer.getItem().getDefinitions().getCertId(), offer.getAmountCompleted());
					offer.setOfferedAmount(offer.getOfferedAmount() - offer.getAmountCompleted());
					offer.setAmountCompleted(0);
					player.getGrandExchange().sendExchangeScreen();
					return;	
				}
				break;
			case SELL:
				if (offer.getAmountCompleted() == 0) {
					player.sendMessage("Your offer has not completed yet. If you wish to stop it, press abort.");
					return;
				}
				if (offer.getAmountCompleted() == offer.getOfferedAmount()) {
					player.getMoneyPouch().add(offer.getOfferedPrice());
					player.getGrandExchange().getOffers()[slot] = null;
					WorldOffers.getOffers().remove(offer);
					player.getGrandExchange().sendExchangeScreen();
					player.getPackets().sendGrandExchangeBar(
							player,
							(Integer) player.getAttributes().get(
									"grand_exchange_slot"), 8,
									offer.getItem().getId(), offer.getOfferedPrice(),
									offer.getOfferedAmount(), offer.getOfferedAmount());
				} else {
					player.getMoneyPouch().add(offer.getFirstPrice() * offer.getAmountCompleted());
					offer.setOfferedAmount(offer.getOfferedAmount() - offer.getAmountCompleted());
					offer.setAmountCompleted(0);
					player.getGrandExchange().sendExchangeScreen();
				}
				return;
			}
			break;
		case OPTION_2:
			if (offer.isFinished()) {
				player.sendMessage("Oops! Too late! Your offer has already been registered in the system.");
				return;
			}
			switch(offer.getType()) {
			case SELL:
				player.getGrandExchange().getOffers()[slot] = null;
				player.getInventory().addItem(offer.getItem().getDefinitions().getCertId() == -1 ? offer.getItem().getId() : offer.getItem().getDefinitions().getCertId(), offer.getOfferedAmount() - offer.getAmountCompleted());
				WorldOffers.getOffers().remove(offer);
				break;
			case BUY:
				player.getGrandExchange().getOffers()[slot] = null;
				player.getMoneyPouch().add(offer.getFirstPrice() * offer.getAmountCompleted());
				WorldOffers.getOffers().remove(offer);
				break;
			}
			break;
		}
		player.getPackets().sendGrandExchangeBar(
				player,
				(Integer) player.getAttributes().get(
						"grand_exchange_slot"), 8,
						offer.getItem().getId(),
						offer.getOfferedPrice(),
						offer.getOfferedAmount(),
						offer.getOfferedAmount());
	}

	@Override
	public void finish() {

	}

}
